package com.sgb.trip.view.activity;

import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.entity.EventConstant;
import com.sgb.link.entity.MainEvent;
import com.sgb.link.router.PathTrip;
import com.sgb.link.utils.RoundDialogHelper;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripActivityChoosePassengerBinding;
import com.sgb.trip.entity.PassengerInfoEntity;
import com.sgb.trip.view.adapter.PassengerInfoChooseAdapter;
import com.sgb.trip.viewmodel.FlightChoosePassengerModel;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/10/9
 * Author:黄淋
 * Description:选择乘机人页面
 */
@Route(path = PathTrip.FlightChoosePassengerActivity)
public class FlightChoosePassengerActivity extends BaseActivity<FlightChoosePassengerModel, TripActivityChoosePassengerBinding> {
    PassengerInfoChooseAdapter mAdapter;

    @Autowired(name = RouterKey.DATA, desc = "选中的乘客列表")
    ArrayList<PassengerInfoEntity> mListPassengers;//
    @Autowired(name = RouterKey.BOOLEAN, desc = "是否修改了乘客列表")
    boolean isChangePassengerList;

    @Override
    protected int layoutId() {
        return R.layout.trip_activity_choose_passenger;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(FlightChoosePassengerModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        mBinding.actionBarChoosePassenger.setMenuFunctionClick(this);
        mBinding.actionBarChoosePassenger.setOnBackClickListener(this);
        mBinding.tvConfirmPassengers.setOnClickListener(this);
        mBinding.tvSearchPassenger.setOnClickListener(this);

        initAdapter();

        mViewModel.ldPassengerData.observe(this, list -> {
            if (mListPassengers != null && mListPassengers.size() > 0) {
                for (int i = 0; i < mListPassengers.size(); i++) {
                    for (int a = 0; a < list.size(); a++) {
                        if (mListPassengers.get(i).getIdCard().equals(list.get(a).getIdCard())) {
                            list.get(a).setSelected(true);
                        }
                    }
                }
            }
            mAdapter.setDatas(list);

            List<PassengerInfoEntity> checkData = new ArrayList<>();
            SparseBooleanArray checkList = new SparseBooleanArray();
            for (int i = 0; i < list.size(); i++) {
                checkList.put(i, list.get(i).isSelected());
                if (list.get(i).isSelected()) {
                    checkData.add(list.get(i));
                }
            }
            mAdapter.setCheckDefultDatas(checkData, checkList);

        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mViewModel.getPassengersList("");
    }

    private void initAdapter() {
        mBinding.rvPassenger.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvPassenger.setLoadingMoreEnabled(false);
        mBinding.rvPassenger.setPullRefreshEnabled(false);
        mAdapter = new PassengerInfoChooseAdapter(this, null);
        mAdapter.setMultiSelect(true);
        mAdapter.setClickDelay(false);
        mAdapter.setOnClickItem(new PassengerInfoChooseAdapter.OnClickItem() {
            @Override
            public void onEdit(PassengerInfoEntity passengerInfoEntity) {
                ARouter.getInstance().build(PathTrip.FlightAddPassengerActivity).withBoolean(RouterKey.BOOLEAN, true).withSerializable(RouterKey.DATA, passengerInfoEntity).navigation();
            }
        });
        mAdapter.setOnItemClick2(new BaseRecycleAdapter.OnItemClick2<PassengerInfoEntity>() {
            @Override
            public void onItemClick(PassengerInfoEntity passengerInfoEntity, int position, ItemViewHolder view) {
                if (mAdapter.getCheckDatas().size() >= 10) {
                    mAdapter.getCheckDatas().remove(passengerInfoEntity);
                    RoundDialogHelper.showSingleDialog(FlightChoosePassengerActivity.this, "温馨提示", "单次订票人数不能超过9个，请分开下单", "知道了", true, R.color.color_5792FD, new RoundDialogHelper.DialogFunction() {
                        @Override
                        public void normalRoundDialog() {

                        }
                    });
                } else {
                    passengerInfoEntity.setSelected(!view.getItem().isSelected());
                }


            }
        });
        mBinding.rvPassenger.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_back_red) {//返回
            if (isChangePassengerList) {
                EventBus.getDefault().post(MainEvent.make().setMessageTag(EventConstant.EVENT_TRIP_EDIT_PASSENGER_REFRESH_ORDER));
            }
            finish();
        } else if (view.getId() == R.id.right_view_red) {//新增
            ARouter.getInstance().build(PathTrip.FlightAddPassengerActivity).withBoolean(RouterKey.BOOLEAN, false).navigation();
        } else if (view.getId() == R.id.tv_confirm_passengers) {//确定
            if (mListPassengers == null)
                mListPassengers = new ArrayList<>();
            mListPassengers.addAll(mAdapter.getCheckDatas());
            if (mListPassengers == null || mListPassengers.size() == 0) {
                MToast.showToast(FlightChoosePassengerActivity.this, "请选择乘机人");
                return;
            }
            EventBus.getDefault().post(MainEvent.make().setMessageTag(EventConstant.EVENT_TRIP_CHOOSE_PASSENGER).setMessageData(mAdapter.getDatas()));
            finish();
        } else if (view.getId() == R.id.tv_search_passenger) {//搜索
            mViewModel.getPassengersList(mBinding.editSearchName.getText().toString().trim());
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (isChangePassengerList) {
                EventBus.getDefault().post(MainEvent.make().setMessageTag(EventConstant.EVENT_TRIP_EDIT_PASSENGER_REFRESH_ORDER));
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
